<?php defined('SYSPATH') or die();?>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>门票价格范围分类-思途CMS{$coreVersion}</title>
    {template 'stourtravel/public/public_min_js'}
    {php echo Common::getCss('style.css,base.css,base_new.css'); }
    {php echo Common::getScript("template.js"); }
</head>
<style>
    .x-action-col-icon {
        height: 16px;
        width: 16px;
        cursor: pointer;
    }
    @keyframes spin {
        0% { transform: rotate(0deg); }
        100% { transform: rotate(360deg); }
    }
    .suit-edit-block{
        position:relative;
        min-height: 120px;
    }
    .suit-edit-block.active:before {
        content: '';
        background-image: url('/res/images/loading_new.gif');
        background-repeat: no-repeat;
        width: 32px;
        height: 32px;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-top: -16px; /* 一半的高度 */
        margin-left: -16px; /* 一半的宽度 */
        animation: spin 2s linear infinite; /* 旋转动画，速度较慢 */
        z-index: 1;
        background-size: contain;
    }
</style>
<body>
<table class="content-tab">
    <tr>
        <td width="119px" class="content-lt-td" valign="top">
            {template 'stourtravel/public/leftnav'}
            <!--右侧内容区-->
        </td>
        <td valign="top" class="content-rt-td" style="overflow:auto;">
            <div class="cfg-header-bar">
                {template 'admin/ferry/attrid/header_tab'}
                <a href="javascript:;" class="fr btn btn-primary radius mt-6 mr-10" onclick="window.location.reload()">刷新</a>
                <a href="javascript:;" class="fr btn btn-primary radius mt-6 mr-10" onclick="addRow()">添加</a>
            </div>
            <form id="price_fm">
                <div class="suit-edit-block">
                    <table class="table table-bg table-hover" id="price_tab">
                        <thead>
                        <tr>
                            <th><span class="ml-20">人群名称</span></th>
                            <th>是否显示</th>
                            <th class="text-c">管理</th>
                        </tr>
                        </thead>
                        <tbody id="people_list"></tbody>
                    </table>
                </div>
            </form>
        </td>
    </tr>
</table>
<script type="text/html" id="add_people_template">
    {{if data.length > 0}}
    {{each data as item i}}
    <tr>
        <td class="dayname-td">
            <span class="ml-20">
                <input type="text" name="name" class="input-text w200" data-id="{{item.id}}" value="{{item.name}}">
            </span>
        </td>
        <td>
            {{if item.is_show == 1}}
            <img role="button" alt=""
                 src=""
                 class="x-action-col-icon x-action-col-0 dest-status-ok edit-suit-status-btn" data-id="{{item.id}}" data-isshow="0">
            {{else}}
            <img  role="button" alt=""
                  src=""
                  class="x-action-col-icon x-action-col-0 dest-status-none edit-suit-status-btn" data-id="{{item.id}}" data-isshow="1">
            {{/if}}
        </td>
        <td class="text-c">
            {{if item.is_system == '1'}}
            <span class="c-999">系统配置</span>
            {{else}}
            <a href="javascript:;" class="btn-link" onclick="delRow(this, {{item.id}})" title="删除">删除</a>
            {{/if}}
        </td>
    </tr>
    {{/each}}
    {{else}}
    <tr class="suit-edit-tb">
        <td class="text-c" colspan="3">未找到相关数据</td>
    </tr>
    {{/if}}
</script>
</body>
<script type="text/javascript">
    var delpic ="{php echo Common::getIco('del');}";

    getList();
    // 获取列表
    function getList() {
        $('.suit-edit-block').addClass('active');
        $.post(SITEURL + "ferry/admin/ferry/ajax_people/action/read", {}, function (res) {
            $('.suit-edit-block').removeClass('active');
            var html = template("add_people_template", res);
            $("#people_list").html(html);
        }, 'json')
    }

    // 添加一行
    function addRow() {
        // 添加一个人群
        ST.Util.showLoading('保存中', 10000);
        $.post(SITEURL + "ferry/admin/ferry/ajax_people/action/add", {}, function (res) {
            if (res.status) {
                ST.Util.showSuccessMsg("新增成功", 1000);
                setTimeout(function () {
                    getList();
                }, 1000);
            } else {
                ST.Util.showMsg("{__('norightmsg')}", 5, 1000);
            }
        }, 'json')
    }

    // 保存
    function rowSave(id, field, value) {
        ST.Util.showLoading('保存中', 10000);
        $.post(SITEURL + "ferry/admin/ferry/ajax_people/action/update", {id, field, value}, function (res) {
            if (res.status) {
                ST.Util.showSuccessMsg("保存成功", 1000);
                setTimeout(function () {
                    getList();
                }, 1000);
            } else {
                ST.Util.showFailureMsg(res.msg);
            }
        }, 'json')
    }

    $("#people_list").on('click', '.edit-suit-status-btn', function (e) {
        // 状态编辑
        var id = $(this).data('id'),
            value = $(this).data('isshow');
        rowSave(id, 'is_show', value);
    }).on('blur', 'input[name=name]', function () {
        var id = $(this).data('id'),
            value = $(this).val();

        value = $.trim(value);
        if (value) {
            rowSave(id, 'name', value);
        } else {
            ST.Util.showFailureMsg('人群名称不能为空');
            setTimeout(function () {
                getList();
            }, 1000);
        }
    })

    // 删除一个人群
    function delRow(dom,id)
    {
        ST.Util.confirmBox('提示', '确认删除这个人群吗?', function () {
            ST.Util.showLoading('保存中', 10000);
            $.ajaxform({
                url: SITEURL + "ferry/admin/ferry/ajax_people/action/del",
                method: "POST",
                data: {id: id},
                dataType: "JSON",
                success: function (data) {
                    ST.Util.hideMsgBox();
                    if (data.status) {
                        $(dom).parents('tr').first().remove();
                    } else {
                        ST.Util.showFailureMsg(data.msg);
                    }
                }
            });
        });
    }
</script>
</html>
